﻿using System;
using System.Collections.Generic;
using ParaEditor.Common;
using ParaEditor.Domain.Entities;
using System.Collections.ObjectModel;
using System.Collections;
using NHibernate;

namespace ParaEditor.DAO.ParaDAO
{
    public class BenfInfoDAO:BaseDAO
    {
        private static BenfInfo param = new BenfInfo();

        public BenfInfoDAO()
            : base(param)
        {
        }


        public ObservableCollection<BenfInfo> GetCurrentBenfInfo()
        {
            ObservableCollection<BenfInfo> list = new ObservableCollection<BenfInfo>();
            string sqlText = "select * from tbl_metro_benf_info where benf_ver = (select max(benf_ver) from tbl_metro_benf_info)";
            ISQLQuery query = Session.CreateSQLQuery(sqlText).AddEntity(param.GetType());
            IList<BenfInfo> iList = query.List<BenfInfo>();
            foreach (BenfInfo item in iList)
            {
                list.Add(item);
            }
            return list;
        }

        public ResultValue BatchCreateObject(IList<BenfInfo> objs)
        {
            ResultValue rValue = new ResultValue();
            try
            {
                int count = objs.Count;
                for (int i = 0; i < count; i++)
                {
                    Session.Merge(objs[i]);
                    if (i % 100 == 0)
                    {
                        Session.Flush();
                        Session.Clear();
                    }
                }
                rValue.Code = 0;
            }
            catch (Exception e)
            {
                rValue.Code = Int32.Parse(ErrorFlag.SAVE_ERROR.Split(':')[0]);
                rValue.Msg = e.Message;
            }
            finally
            {
                Session.Flush();
                Session.Clear();
            }
            return rValue;
        }
    }
}
